package tryonce.dao;

import tryonce.entry.Store;

import java.util.*;

public class StoreDao {
    private Set<Store> set = new TreeSet<>();//创建商品
    static Scanner sc = new Scanner(System.in);//创建扫描器

    //初始化
    public StoreDao() {
        set.add(new Store(1111, "蜘蛛王皮鞋", 238.0, "黑色", 42, 100000));
        set.add(new Store(3333, "ThinkPadx240", 5939.0, "黑色", 12, 50));
        set.add(new Store(2222, "iphone15plus", 7999.0, "白色", 5, 5000));
    }

    //查询商品
    public Set<Store> getAll() {
        return new TreeSet<>(set);//返回商品副本，防止数据被修改
    }

    //根据商品编号查询商品
    public Store findById(Integer id) {
        for (Store store : set) {//遍历取值
            if (store.getId().equals(id)) {//商品编号相等
                return store;//返回商品
            }
        }
        return null;//商品不存在
    }

    //添加商品
    public boolean add(Store store) {
        if (findById(store.getId()) != null) {
            return false;
        }
        return set.add(store);
    }
}
